﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using FRL.DebtChased.Repository;
using FRL.DebtChased.Silverlight.Library.RichContentService;

namespace FRL.DebtChased.Silverlight.Library
{

	/// <summary>
	/// Declaration of PickCommBodyWnd class
	/// Child window for selecting a comm-body and
	/// none or more sections within the comm-body
	/// </summary>
	public partial class PickCommBodyWnd : ChildWindow
	{

		/////////////////////////////////////////////////////////////
		// Attributes

		private bool m_bBlockEvents = true;

		/////////////////////////////////////////////////////////////
		// Construction

		public PickCommBodyWnd()
		{

			// This constructor is provided to help development
			// This constructor should never be called from code

			// Load xaml resoucres
			InitializeComponent();

		}

		public PickCommBodyWnd(
			List<CommBodySummaryTransport> commBodyList,
			Definitions.eCommBodyType commBodyType,
			int iCommBodyId,
			List<String> strSectionList
		)
		{
#if DEBUG
			Debug.Assert(commBodyType != Definitions.eCommBodyType.NotSet);
#endif // #if DEBUG

			// Load xaml resoucres
			InitializeComponent();

			// Load drop-down
			cboBody.Items.Add( new ComboBoxObject( Definitions.NotSet, 0, null ) );
			foreach (CommBodySummaryTransport cbtLoop in commBodyList)
			{

				// If type matches...
				if (cbtLoop.CommBodyType == (int)commBodyType)
				{

					// Add to combobox box
					cboBody.Items.Add(new ComboBoxObject(cbtLoop.Name, cbtLoop.CommBodyId, cbtLoop));

				}

			}
			cboBody.SelectedIndex = 0;
			ComboBoxObject.SelectByItemData(cboBody,iCommBodyId);

			// Set lists
			CommBodySummaryTransport cbt = (cboBody.SelectedItem as ComboBoxObject).Tag as CommBodySummaryTransport;
			if(cbt != null)
			{
				for (int i1 = 0; i1 < cbt.SectionList.Count; i1++)
				{
					if (strSectionList.Contains(cbt.SectionList[i1]))
						lstSelected.Items.Add(cbt.SectionList[i1]);
					else
						lstAvailable.Items.Add(cbt.SectionList[i1]);
				}
			}

			// Update flag
			m_bBlockEvents = false;

			// Setup controls
			_setupControls();

		}

		/////////////////////////////////////////////////////////////
		// Event Handlers

		private void OKButton_Click(object sender, RoutedEventArgs e)
		{

			// Close window
			this.DialogResult = true;

		}

		private void CancelButton_Click(object sender, RoutedEventArgs e)
		{

			// Close window
			this.DialogResult = false;

		}

		private void cboBody_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{

			// Shortcuts
			if (m_bBlockEvents)
				return;

			// Block events
			m_bBlockEvents = true;
			{

				// Set lists
				lstAvailable.Items.Clear();
				lstSelected.Items.Clear();
				CommBodySummaryTransport cbt = (cboBody.SelectedItem as ComboBoxObject).Tag as CommBodySummaryTransport;
				if (cbt != null)
				{
					for (int i1 = 0; i1 < cbt.SectionList.Count; i1++)
						lstAvailable.Items.Add(cbt.SectionList[i1]);
				}

			}
			m_bBlockEvents = false;

			// Setup controls
			_setupControls();

		}

		private void lstAvailable_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{

			// Shortcuts
			if (m_bBlockEvents)
				return;

			// Setup controls
			_setupControls();

		}

		private void lstSelected_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{

			// Shortcuts
			if (m_bBlockEvents)
				return;

			// Setup controls
			_setupControls();

		}

		private void butAdd_Click(object sender, RoutedEventArgs e)
		{

			// Swap selected items
			_swapSelectedItem(lstAvailable, lstSelected);

		}

		private void butRemove_Click(object sender, RoutedEventArgs e)
		{

			// Swap selected items
			_swapSelectedItem(lstSelected, lstAvailable);

		}

		/////////////////////////////////////////////////////////////
		// Operations

		private void _swapSelectedItem(ListBox lstFrom, ListBox lstTo)
		{

			// Swap selected items from one ListBox to another

			// Declaration of local variables
			int i1 = 0;

			// Block updates 
			m_bBlockEvents = true;
			{

				// Get selected objects
				List<object> obList = new List<object>();
				for( i1=0; i1<lstFrom.SelectedItems.Count; i1++ )
					obList.Add(lstFrom.SelectedItems[i1]);

				// Remove selected items
				for (i1 = 0; i1 < obList.Count; i1++)
					lstFrom.Items.Remove(obList[i1]);

				// Add to the other list
				for( i1=0; i1<obList.Count; i1++ )
					lstTo.Items.Add(obList[i1]);

			}
			m_bBlockEvents = false;

			// Setup controls
			_setupControls();

		}

		private void _setupControls()
		{

			// Enable/disable controls
			butAdd.IsEnabled = (lstAvailable.SelectedItems.Count > 0);
			butRemove.IsEnabled = (lstSelected.SelectedItems.Count > 0);

		}

		public int GetCommBodyId()
		{

			// Get int from control
			return (cboBody.SelectedItem as ComboBoxObject).ItemData;

		}

		public List<string> GetSectionList()
		{

			// Declare return variable
			List<string> Ftmp = new List<string>();

			// Loop through selected sections
			for (int i1 = 0; i1 < lstSelected.Items.Count; i1++)
				Ftmp.Add(lstSelected.Items[i1].ToString());

			// Return variable
			return Ftmp;

		}

	}

}

